package com.corticon.dialogs.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.corticon.dialogs.dao.RegionDAO;
import com.corticon.dialogs.model.Region;
import com.corticon.dialogs.model.Region;
import com.corticon.dialogs.util.Pager;

@Repository("regionDAO")
public class RegionHibernateDAO extends GenericHibernateDAO<Region, String> implements RegionDAO{
	private String countByUserGroup =
        "select count(*) from Region theme where theme.userGroup.name='ALL' or theme.userGroup.name = :userGroup";
	private String countByUserGroupAll =
        "select count(*) from Region theme where theme.userGroup.name="+"'ALL'";
  
    private String queryByUserGroup = "from Region theme where theme.userGroup.name = :userGroup or theme.userGroup.name="+"'ALL'";
    private String queryByUserGroupAll = "from Region theme where theme.userGroup.name = 'ALL'";

    @Override
    public long getCountByUserGroup(String userGroup) {
        return (Long) getSession().createQuery(countByUserGroup).setParameter("userGroup",
            userGroup).uniqueResult();
    }

    @Override
    public List<Region> findPageByUserGroup(String userGroup, Pager pager) {
        List<Region> themes = new ArrayList<Region>();

        themes = getSession().createQuery(queryByUserGroup).setParameter("userGroup", userGroup).
            setFirstResult(pager.getFirstRecord()).setMaxResults(pager.getPageSize()).list();

        return themes;
    }
    @Override
    public long getCountByUserGroupAll(String userGroup) {
        return (Long) getSession().createQuery(countByUserGroupAll).uniqueResult();
    }

    @Override
    public List<Region> findPageByUserGroupAll(String userGroup, Pager pager) {
        List<Region> themes = new ArrayList<Region>();

        themes = getSession().createQuery(queryByUserGroupAll).
            setFirstResult(pager.getFirstRecord()).setMaxResults(pager.getPageSize()).list();

        return themes;
    }
}
